import { queyEnumByMultiTableName } from '@/api/api.js';
import _ from 'underscore';
import Vue from 'vue';

function returnDataValue(val, tablename) {
  let str = `${tablename}-${val}`;
  return window.localStorage.getItem(str)
    ? JSON.parse(window.localStorage.getItem(str))
    : JSON.parse('[]');
}
export const myEnumsMixins = {
  data() {
    return {
      selectEnums: {
        nice_inner_user: {
          education: returnDataValue('education', 'nice_inner_user'), // 学历
          status: returnDataValue('status', 'nice_inner_user'), // 状态
          position_id: returnDataValue('position_id', 'nice_inner_user'), // 岗位
        },
        nice_teacher_info: {
          speaker_rating: returnDataValue(
            'speaker_rating',
            'nice_teacher_info'
          ), // 讲师评级
          speaker_grade: returnDataValue('speaker_grade', 'nice_teacher_info'), // 主讲年级
          speaker_subject: returnDataValue(
            'speaker_subject',
            'nice_teacher_info'
          ), // 任教学科
        },
        nice_teacher_tutor: {
          tutor_type: returnDataValue('tutor_type', 'nice_teacher_tutor'), // 辅导老师签约
        },
        nice_teacher_cert: {
          qualification_type: returnDataValue(
            'qualification_type',
            'nice_teacher_cert'
          ), // 教师资格种类
          teach_subject: returnDataValue('teach_subject', 'nice_teacher_cert'), // 任教学科
        },
        nice_school: {
          school_type: returnDataValue('school_type', 'nice_school'), // 学校性质
          level: returnDataValue('level', 'nice_school'), // 学校等级
          student_scale: returnDataValue('student_scale', 'nice_school'), // 学生规模
          is_open: returnDataValue('is_open', 'nice_school'), // 状态,
          coverage_grade: returnDataValue('coverage_grade', 'nice_school'), // 覆盖年级
          teacher_scale: returnDataValue('teacher_scale', 'nice_school'), // 教师规模
        },
        sys_user: {
          user_type: returnDataValue('user_type', 'sys_user'), // 用户类型
        },
        nice_student: {
          status: returnDataValue('status', 'nice_student'), // 状态
          source: returnDataValue('source', 'nice_student'), // 来源
        },
        nice_order: {
          status: returnDataValue('status', 'nice_order'), // 订单状态
          is_use_coupon: returnDataValue('is_use_coupon', 'nice_order'), // 优惠方式
        },
        nice_pay_info: {
          pay_way: returnDataValue('pay_way', 'nice_pay_info'), // 支付方式
        },
        nice_exam: {
          exam_type: returnDataValue('exam_type', 'nice_exam'), // 考试类型
        },
        nice_course: {
          subject: returnDataValue('subject', 'nice_course'), // 科目
          grade: returnDataValue('grade', 'nice_course'), // 年级
          term: returnDataValue('term', 'nice_course'), // 学期
          type: returnDataValue('type', 'nice_course'), // 课程分类
          status: returnDataValue('status', 'nice_course'), // 课程状态
        },
        nice_user_base: {
          user_channel: returnDataValue('user_channel', 'nice_user_base'), // 用户渠道
        },
        nice_question_lib_question: {
          sub_type: returnDataValue('sub_type', 'nice_question_lib_question'), // 题型
        },
      },
    };
  },
  created() {
    // this.queyEnumByMultiTableName()
  },
  filters: {
    G(value, data) {
      let res = _.filter(data, function(item) {
        return item.value === value;
      });
      if (res.length) {
        return res[0].display_value;
      } else {
        return '';
      }
    },
  },
  methods: {
    async queyEnumByMultiTableName() {
      if (!this.selectEnums.nice_inner_user.education.length) {
        let res = await queyEnumByMultiTableName({
          param: [
            'nice_inner_user',
            'nice_teacher_info',
            'nice_teacher_cert',
            'nice_school',
            'sys_user',
            'nice_order',
            'nice_student',
            'nice_pay_info',
            'nice_course',
            'nice_user_base',
            'nice_question_lib_question',
            'nice_teacher_tutor',
            'nice_exam',
          ],
        });
        if (res[0]) {
          return;
        }
        let data = res[1].data;
        Object.keys(this.selectEnums).forEach((keys) => {
          for (let i = 0; i < data.length; i++) {
            Object.keys(data[i]).forEach((ele) => {
              if (keys === ele) {
                Object.keys(this.selectEnums[keys]).forEach((key) => {
                  data[i][ele].forEach((item) => {
                    if (item.fieldName === key) {
                      let arr = item.child.map((child) => {
                        return {
                          label: child.display_value,
                          value: child.t_id,
                          real_value: child.real_value,
                          child: child.child,
                          display_value: child.display_value,
                        };
                      });
                      this.returnFilter(arr, key, keys);
                    }
                  });
                });
              }
            });
          }
        });
      }
    },
    returnFilter(arr, key, keys) {
      window.localStorage.setItem(
        `${keys}-${key}`,
        arr && arr.length ? JSON.stringify(arr) : JSON.stringify([])
      );
      console.log('key==>', key);
      if (key === 'sub_type') {
        let mixin_enumerationfieldData = JSON.parse(
          window.localStorage.getItem('nice_question_lib_question-sub_type')
        );
        let Obj = {};
        mixin_enumerationfieldData.forEach((ele) => {
          if (ele.real_value) {
            Obj[`${ele.real_value}`] = ele.value;
          }
        });
        window.enumeration = Object.assign({}, window.enumeration, Obj);
        this.mixin_enumerationfieldData = window.enumeration;
        Vue.prototype.$mixin_enumerationfieldData = this.mixin_enumerationfieldData;
        window.localStorage.setItem(
          `mixin_enumerationfieldData`,
          JSON.stringify(window.enumeration)
        );
      }
    },
  },
};

export default {
  myEnumsMixins,
};
